using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;

namespace AntEater.ComponentModel.BuildElements.Design
{

    /// <summary>
    /// Provides a type converter to convert multi line strings to and from various other representations.
    /// </summary>
    public class TextConverter:TypeConverter
    {

        /// <summary>
        /// Converts the given value object to the specified destination type.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
        /// <param name="culture">The culture to which value will be converted.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to convert. This parameter must inherit from <see cref="T:System.Collections.ICollection"></see>.</param>
        /// <param name="destinationType">The <see cref="T:System.Type"></see> to convert the value to.</param>
        /// <returns>
        /// An <see cref="T:System.Object"></see> that represents the converted value.
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">destinationType is null. </exception>
        /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {

            if (destinationType == typeof(string)) {

                string multiLineString = value as string;

                if (value != null) {
                    int startIndex = -1;
                    int stopIndex = -1;

                    //Find the first non whitespace character
                    foreach (char character in multiLineString) {
                        startIndex++;
                        if (!char.IsWhiteSpace(character)) {
                            break;
                        }
                    }

                    //Find the first new line
                    stopIndex = multiLineString.IndexOf(Environment.NewLine, startIndex);

                    int lenght = stopIndex - startIndex;

                    if (lenght > 0) {

                        //Display only 20 charss
                        if (lenght > 20) {
                            lenght = 20;
                        }

                        return multiLineString.Substring(startIndex, lenght) + "..." ;
                    }
                    else {
                        return multiLineString;
                    }
                }
                else {
                    return value;
                }
            }
            else {
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

        /// <summary>
        /// Converts the given object to the type of this converter, using the specified context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use as the current culture.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
        /// <returns>
        /// An <see cref="T:System.Object"></see> that represents the converted value.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
            return base.ConvertFrom(context, culture, value);
        }
    }
}
